{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-1 面部检测\n",
    "import requests\n",
    "# json\n",
    "import json\n",
    "\n",
    "# set to your own subscription key value\n",
    "subscription_key = \"74e44103f455499e95f90eb4a63ef3cd\"\n",
    "assert subscription_key\n",
    "\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://1119api.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "\n",
    "# 请求正文body\n",
    "image_url = 'https://images.gitee.com/uploads/images/2020/1024/191514_25b6308f_5329225.jpeg \"API picture1.jpg\"'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数parameters\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->bytes\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# response.content\n",
    "results = response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'cde1e590-9d4c-4b9a-a783-026fea687223',\n",
       "  'faceRectangle': {'top': 138, 'left': 119, 'width': 197, 'height': 197},\n",
       "  'faceAttributes': {'smile': 0.0,\n",
       "   'headPose': {'pitch': -0.8, 'roll': -0.9, 'yaw': 28.8},\n",
       "   'gender': 'male',\n",
       "   'age': 1.0,\n",
       "   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 0.994,\n",
       "    'sadness': 0.005,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'medium', 'value': 0.57},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.57},\n",
       "   'noise': {'noiseLevel': 'low', 'value': 0.26},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.08,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'brown', 'confidence': 0.85},\n",
       "     {'color': 'blond', 'confidence': 0.74},\n",
       "     {'color': 'black', 'confidence': 0.43},\n",
       "     {'color': 'red', 'confidence': 0.43},\n",
       "     {'color': 'other', 'confidence': 0.34},\n",
       "     {'color': 'gray', 'confidence': 0.17},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "# 1、create  列表\n",
    "# faceListId\n",
    "faceListId = \"xiaoyan02\"\n",
    "create_facelists_url = 'https://1119api.cognitiveservices.azure.com/face/v1.0/facelists/{}'\n",
    "subscription_key = \"511da085497f43b8bcd037f002cff644\"\n",
    "assert subscription_key\n",
    "\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "data = {\n",
    "        \"name\": \"API\",\n",
    "        \"userData\": \"boy\",\n",
    "        \"recognitionModel\": \"recognition_01\"  \n",
    "}\n",
    "\n",
    "r_create = requests.put(create_facelists_url.format(faceListId),headers=headers,json=data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 已经成功创建facelist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 检查你的facelist的信息\n",
    "\n",
    "get_facelist_url = \"https://1119api.cognitiveservices.azure.com/face/v1.0/facelists/%s\"%(faceListId)\n",
    "r_get_facelist = requests.get(get_facelist_url.format(faceListId),headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [],\n",
       " 'faceListId': 'xiaoyan02',\n",
       " 'name': 'API',\n",
       " 'userData': 'boy'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 增加脸\n",
    "\n",
    "add_face_url = \"https://1119api.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces\"\n",
    "\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "img_url = \"https://thumbnail0.baidupcs.com/thumbnail/2fd531cdav98feb7577d9ea3e5fa1ab4?fid=4040597630-250528-685360770521682&time=1603537200&rt=sh&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-VUtw53qKpJorRVKD7PaTQo2SVyY%3D&expires=8h&chkv=0&chkbd=0&chkpc=&dp-logid=6906934560771384535&dp-callid=0&size=c710_u400&quality=100&vuk=-&ft=video\"\n",
    "\n",
    "params_add_face={\n",
    "    \"userData\":\"小娃子\"\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaceId': '739cb0c1-77e2-4173-bc70-da9e974b5770'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检查facelist的信息\n",
    "r_add_face.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装成函数方便添加图片\n",
    "def AddFace(img_url=str,userData=str):\n",
    "    add_face_url = \"https://1119api.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces\"\n",
    "    assert subscription_key\n",
    "    headers = {\n",
    "        # Request headers\n",
    "        'Content-Type': 'application/json',\n",
    "        'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "    }\n",
    "    img_url = img_url\n",
    "\n",
    "    params_add_face={\n",
    "        \"userData\":userData\n",
    "    }\n",
    "    r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "    return r_add_face.status_code#返回出状态码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"https://thumbnail0.baidupcs.com/thumbnail/2fb8c73a8vd9c8c729b29d34a2a2e852?fid=4040597630-250528-430636301132561&time=1603530000&rt=sh&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-0BS9OD%2BZZ%2BWkehpd%2Bns7vCmFFzo%3D&expires=8h&chkv=0&chkbd=0&chkpc=&dp-logid=6905089593336113629&dp-callid=0&size=c710_u400&quality=100&vuk=-&ft=video\",\"小娃子02\")\n",
    "AddFace(\"https://thumbnail0.baidupcs.com/thumbnail/9f34b75e4v0376ee224ffcd398ea604c?fid=4040597630-250528-844400763142722&time=1603530000&rt=sh&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-lBywIqhBSRzz2RdXGiBpynkLBwQ%3D&expires=8h&chkv=0&chkbd=0&chkpc=&dp-logid=6905293556874245946&dp-callid=0&size=c710_u400&quality=100&vuk=-&ft=video\",\"小娃子03\")\n",
    "AddFace(\"https://thumbnail0.baidupcs.com/thumbnail/92abcfa05p3297dd589ccc3881cad152?fid=4040597630-250528-702459409709855&time=1603530000&rt=sh&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-fEcT%2FWZ4w7IONhtIIe0eiBNij9M%3D&expires=8h&chkv=0&chkbd=0&chkpc=&dp-logid=6905306858574734106&dp-callid=0&size=c710_u400&quality=100&vuk=-&ft=video\",\"小娃子04\")\n",
    "AddFace(\"https://thumbnail0.baidupcs.com/thumbnail/d848a8cfft7d17171340a98e24d4ba35?fid=4040597630-250528-376397805591258&time=1603530000&rt=sh&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-mI4ypLvPdIwTYSq4iHNw%2BBiJo9I%3D&expires=8h&chkv=0&chkbd=0&chkpc=&dp-logid=6905322049606199578&dp-callid=0&size=c710_u400&quality=100&vuk=-&ft=video\",\"小娃子05\")\n",
    "AddFace(\"https://thumbnail0.baidupcs.com/thumbnail/80de5237fh9577120f4b379a257d57f4?fid=4040597630-250528-1110811697339008&time=1603530000&rt=sh&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-RKDRZk9Sb6VLN68Q4sdubsWvwME%3D&expires=8h&chkv=0&chkbd=0&chkpc=&dp-logid=6905334333468672516&dp-callid=0&size=c710_u400&quality=100&vuk=-&ft=video\",\"小娃子06\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '739cb0c1-77e2-4173-bc70-da9e974b5770',\n",
       "   'userData': '小娃子'},\n",
       "  {'persistedFaceId': '2c2d97ed-d371-4c2f-88ce-a1d8df88eb55',\n",
       "   'userData': '小娃子02'},\n",
       "  {'persistedFaceId': '810f6bbc-4961-4902-9b63-099f9c592f54',\n",
       "   'userData': '小娃子03'},\n",
       "  {'persistedFaceId': 'a8210e71-258c-4a28-a7c7-cb404e3f9f36',\n",
       "   'userData': '小娃子04'},\n",
       "  {'persistedFaceId': 'f76672da-e9f2-4f46-a682-100b782ae9ef',\n",
       "   'userData': '小娃子05'},\n",
       "  {'persistedFaceId': 'da7237d9-29f9-4d90-a9b4-83f23eec2e83',\n",
       "   'userData': '小娃子06'}],\n",
       " 'faceListId': 'xiaoyan02',\n",
       " 'name': 'API',\n",
       " 'userData': 'boy'}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检查你的facelist的信息\n",
    "get_facelist_url =\"https://1119api.cognitiveservices.azure.com/face/v1.0/facelists/{}\"\n",
    "r_get_facelist = requests.get(get_facelist_url.format(faceListId),headers=headers,params=data)\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '61a6f856-c051-406a-a0b9-f2868d4e74a5',\n",
       "  'faceRectangle': {'top': 91, 'left': 128, 'width': 129, 'height': 129},\n",
       "  'faceAttributes': {'smile': 0.003,\n",
       "   'headPose': {'pitch': -7.0, 'roll': 11.4, 'yaw': 19.8},\n",
       "   'gender': 'male',\n",
       "   'age': 0.0,\n",
       "   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.003,\n",
       "    'neutral': 0.882,\n",
       "    'sadness': 0.003,\n",
       "    'surprise': 0.112},\n",
       "   'blur': {'blurLevel': 'medium', 'value': 0.71},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.49},\n",
       "   'noise': {'noiseLevel': 'medium', 'value': 0.57},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.51,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'blond', 'confidence': 0.84},\n",
       "     {'color': 'brown', 'confidence': 0.72},\n",
       "     {'color': 'black', 'confidence': 0.6},\n",
       "     {'color': 'gray', 'confidence': 0.46},\n",
       "     {'color': 'other', 'confidence': 0.3},\n",
       "     {'color': 'red', 'confidence': 0.12},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检测人脸的id\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://1119api.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "\n",
    "# 请求正文\n",
    "image_url = 'https://thumbnail0.baidupcs.com/thumbnail/2fb8c73a8vd9c8c729b29d34a2a2e852?fid=4040597630-250528-430636301132561&time=1603530000&rt=sh&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-0BS9OD%2BZZ%2BWkehpd%2Bns7vCmFFzo%3D&expires=8h&chkv=0&chkbd=0&chkpc=&dp-logid=6905089593336113629&dp-callid=0&size=c710_u400&quality=100&vuk=-&ft=video'\n",
    "subscription_key = '74e44103f455499e95f90eb4a63ef3cd'\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 选择model\n",
    "    'recognitionModel':'recognition_03',#此参数需与facelist参数一致\n",
    "    'detectionModel':'detection_01',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->字符串\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "findsimilars_url = \"https://1119api.cognitiveservices.azure.com/face/v1.0/findsimilars\"\n",
    "\n",
    "# 请求正文 faceId需要先检测一张照片获取\n",
    "data_findsimilars = {\n",
    "    \"faceId\":\"cde1e590-9d4c-4b9a-a783-026fea687223\",#取上方的faceID\n",
    "    \"faceListId\": \"xiaoyan02\",\n",
    "    \n",
    "    \"maxNumOfCandidatesReturned\": 10,\n",
    "    \"mode\": \"matchFace\"#matchPerson #一种为验证模式，一种为相似值模式\n",
    "    }\n",
    "\n",
    "r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'persistedFaceId': '2c2d97ed-d371-4c2f-88ce-a1d8df88eb55',\n",
       "  'confidence': 0.86374},\n",
       " {'persistedFaceId': 'a8210e71-258c-4a28-a7c7-cb404e3f9f36',\n",
       "  'confidence': 0.83034},\n",
       " {'persistedFaceId': '810f6bbc-4961-4902-9b63-099f9c592f54',\n",
       "  'confidence': 0.81965},\n",
       " {'persistedFaceId': 'f76672da-e9f2-4f46-a682-100b782ae9ef',\n",
       "  'confidence': 0.78071},\n",
       " {'persistedFaceId': 'da7237d9-29f9-4d90-a9b4-83f23eec2e83',\n",
       "  'confidence': 0.76121},\n",
       " {'persistedFaceId': '739cb0c1-77e2-4173-bc70-da9e974b5770',\n",
       "  'confidence': 0.68418},\n",
       " {'persistedFaceId': '53741c47-0ed7-4d94-8985-2af8d32037fe',\n",
       "  'confidence': 0.68418}]"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>739cb0c1-77e2-4173-bc70-da9e974b5770</td>\n",
       "      <td>小娃子</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2c2d97ed-d371-4c2f-88ce-a1d8df88eb55</td>\n",
       "      <td>小娃子02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>810f6bbc-4961-4902-9b63-099f9c592f54</td>\n",
       "      <td>小娃子03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>a8210e71-258c-4a28-a7c7-cb404e3f9f36</td>\n",
       "      <td>小娃子04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>f76672da-e9f2-4f46-a682-100b782ae9ef</td>\n",
       "      <td>小娃子05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>da7237d9-29f9-4d90-a9b4-83f23eec2e83</td>\n",
       "      <td>小娃子06</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId userData\n",
       "0  739cb0c1-77e2-4173-bc70-da9e974b5770      小娃子\n",
       "1  2c2d97ed-d371-4c2f-88ce-a1d8df88eb55    小娃子02\n",
       "2  810f6bbc-4961-4902-9b63-099f9c592f54    小娃子03\n",
       "3  a8210e71-258c-4a28-a7c7-cb404e3f9f36    小娃子04\n",
       "4  f76672da-e9f2-4f46-a682-100b782ae9ef    小娃子05\n",
       "5  da7237d9-29f9-4d90-a9b4-83f23eec2e83    小娃子06"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#facelist里面的数据\n",
    "adf = pd.json_normalize(r_get_facelist.json()[\"persistedFaces\"])# 升级pandas才能运行\n",
    "adf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2c2d97ed-d371-4c2f-88ce-a1d8df88eb55</td>\n",
       "      <td>0.86374</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>a8210e71-258c-4a28-a7c7-cb404e3f9f36</td>\n",
       "      <td>0.83034</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>810f6bbc-4961-4902-9b63-099f9c592f54</td>\n",
       "      <td>0.81965</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>f76672da-e9f2-4f46-a682-100b782ae9ef</td>\n",
       "      <td>0.78071</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>da7237d9-29f9-4d90-a9b4-83f23eec2e83</td>\n",
       "      <td>0.76121</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>739cb0c1-77e2-4173-bc70-da9e974b5770</td>\n",
       "      <td>0.68418</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>53741c47-0ed7-4d94-8985-2af8d32037fe</td>\n",
       "      <td>0.68418</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId  confidence\n",
       "0  2c2d97ed-d371-4c2f-88ce-a1d8df88eb55     0.86374\n",
       "1  a8210e71-258c-4a28-a7c7-cb404e3f9f36     0.83034\n",
       "2  810f6bbc-4961-4902-9b63-099f9c592f54     0.81965\n",
       "3  f76672da-e9f2-4f46-a682-100b782ae9ef     0.78071\n",
       "4  da7237d9-29f9-4d90-a9b4-83f23eec2e83     0.76121\n",
       "5  739cb0c1-77e2-4173-bc70-da9e974b5770     0.68418\n",
       "6  53741c47-0ed7-4d94-8985-2af8d32037fe     0.68418"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回相似度的数据\n",
    "bdf = pd.json_normalize(r_findsimilars.json())# 升级pandas才能运行\n",
    "bdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2c2d97ed-d371-4c2f-88ce-a1d8df88eb55</td>\n",
       "      <td>小娃子02</td>\n",
       "      <td>0.86374</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>a8210e71-258c-4a28-a7c7-cb404e3f9f36</td>\n",
       "      <td>小娃子04</td>\n",
       "      <td>0.83034</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>810f6bbc-4961-4902-9b63-099f9c592f54</td>\n",
       "      <td>小娃子03</td>\n",
       "      <td>0.81965</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>f76672da-e9f2-4f46-a682-100b782ae9ef</td>\n",
       "      <td>小娃子05</td>\n",
       "      <td>0.78071</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>da7237d9-29f9-4d90-a9b4-83f23eec2e83</td>\n",
       "      <td>小娃子06</td>\n",
       "      <td>0.76121</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>739cb0c1-77e2-4173-bc70-da9e974b5770</td>\n",
       "      <td>小娃子</td>\n",
       "      <td>0.68418</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId userData  confidence\n",
       "1  2c2d97ed-d371-4c2f-88ce-a1d8df88eb55    小娃子02     0.86374\n",
       "3  a8210e71-258c-4a28-a7c7-cb404e3f9f36    小娃子04     0.83034\n",
       "2  810f6bbc-4961-4902-9b63-099f9c592f54    小娃子03     0.81965\n",
       "4  f76672da-e9f2-4f46-a682-100b782ae9ef    小娃子05     0.78071\n",
       "5  da7237d9-29f9-4d90-a9b4-83f23eec2e83    小娃子06     0.76121\n",
       "0  739cb0c1-77e2-4173-bc70-da9e974b5770      小娃子     0.68418"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#合并在一起，得出班级能谁最像你\n",
    "pd.merge(adf, bdf,how='inner', on='persistedFaceId').sort_values(by=\"confidence\",ascending = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除人脸/人脸列表\n",
    "faceListId = \"xiaoyan02\" # 需要删除的人脸列表\n",
    "# 删除列表内人脸\n",
    "delete_face_url = \"https://1119api.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedfaces/{}\"\n",
    "assert subscription_key\n",
    "# 获取上面的persistedFaceId\n",
    "persistedFaceId = r_add_face.json()[\"persistedFaceId\"] \n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "# 注意requests请求为delete\n",
    "r_delete_face = requests.delete(delete_face_url.format(faceListId,persistedFaceId),headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_delete_face"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Detect face 删除列表内人脸列表\n",
    "delete_face_url = \"https://1119api.cognitiveservices.azure.com/face/v1.0/facelists/{}\"\n",
    "assert subscription_key\n",
    "\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "\n",
    "# format内直接删除\n",
    "# 注意requests请求为delete\n",
    "r_delete_facelist = requests.delete(delete_face_url.format(faceListId,persistedFaceId),headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_delete_face"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_delete_facelist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
